#include "cmd.h"
#include "string.h"
#include "stdio.h"
#include "main.h"
#include "stdlib.h"
#include "power.h"

extern void inna_read_status();
extern void power_reset(void);
extern void test_mp5416_get_vol(void);
void power_enable(void);
void power_disable(void);
void mp5416_write_reg(int16_t power_address,int16_t Reg_addr,int16_t snumber,uint8_t* Pow_Data_Buffer );

int do_power_ctl(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	if (argc < 3)
	{
		cmd_usage(cmdtp);
		return -1;
	}
	/*
	if (strncmp(argv[1], "cpu", strlen(argv[1])) == 0)
	{
		if (strncmp(argv[2], "on", strlen(argv[2])) == 0)
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_CPU_Pin, GPIO_PIN_SET);
		else 
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_CPU_Pin, GPIO_PIN_RESET);
	}
	else if (strncmp(argv[1], "apu", strlen(argv[1])) == 0)
	{
		if (strncmp(argv[2], "on", strlen(argv[2])) == 0)
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_APU_Pin, GPIO_PIN_SET);
		else 
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_APU_Pin, GPIO_PIN_RESET);
	}
	else if (strncmp(argv[1], "soc", strlen(argv[1])) == 0)
	{
		if (strncmp(argv[2], "on", strlen(argv[2])) == 0)
			HAL_GPIO_WritePin(GPIOB, EN_3V3_SOC_Pin, GPIO_PIN_SET);
		else 
			HAL_GPIO_WritePin(GPIOB, EN_3V3_SOC_Pin, GPIO_PIN_RESET);
	}
	else if (strncmp(argv[1], "pcie_A", strlen(argv[1])) == 0)
	{
			if (strncmp(argv[2], "on", strlen(argv[2])) == 0)
			HAL_GPIO_WritePin(GPIOB, EN_AVDD08_PCIE_Pin, GPIO_PIN_SET);
		else 
			HAL_GPIO_WritePin(GPIOB, EN_AVDD08_PCIE_Pin, GPIO_PIN_RESET);
	}
	else if (strncmp(argv[1], "pcie_D", strlen(argv[1])) == 0)
	{
		if (strncmp(argv[2], "on", strlen(argv[2])) == 0)
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_PCIE_Pin, GPIO_PIN_SET);
		else 
			HAL_GPIO_WritePin(GPIOA, EN_DVDD08_PCIE_Pin, GPIO_PIN_RESET);
	} 
	else 
		cmd_usage(cmdtp);
	*/
	return 0;
}

int do_pmic_ctl(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	uint8_t ch = 0;
	uint8_t vol = 0;
	
	if (argc < 2)
	{
		cmd_usage(cmdtp);
		return -1;
	}
	
	if (strncmp(argv[1], "reset", strlen(argv[1]) ) == 0)
	{
		printf("Reset PMIC\r\n");
		power_reset();
	}
	else if (strncmp(argv[1], "rd_vol", strlen(argv[1])) == 0)
	{
		printf("Read MP5416's voltage\r\n");
		test_mp5416_get_vol();
	}
	else if (strncmp(argv[1], "set", strlen(argv[1])) == 0)
	{
		if (argc < 4)
		{
			printf("Parameter Error!\r\n");
			return -1;
		}
		ch = (uint8_t)strtol(argv[2],NULL,16);
		vol = (uint8_t)strtol(argv[3],NULL,16);
		vol = vol | 0x80;
		printf("Set pmic ch=%d,vol=0x%x\r\n",ch,vol);
		mp5416_write_reg(MP5416_DEVICE_ADDR,MP5416_VSET1 + ch - 1,1,&vol); 
	}

	
	return 0;
}


int do_vol(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	if (argc < 2)
	{
		cmd_usage(cmdtp);
		return -1;
	}
	
	if (strncmp(argv[1], "rd_all", strlen(argv[1])) == 0)
		inna_read_status();
	
	return 0;
}


int do_power_enable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	printf("Power Enable.\r\n");
	power_enable();
	return 0;
}

int do_power_disable(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	printf("Power Disable.\r\n");
	power_disable();
	return 0;
}

LYNXI_MCU_CMD(
	pwr_ctl, MAX_CMD_ARG_NUM, 1, do_power_ctl,
	"Power control.",
	"pwr_ctl cpu on/off\r\n"
	"pwr_ctl apu on/off\r\n"
	"pwr_ctl soc on/off\r\n"
	"pwr_ctl pcie_A on/off\r\n"
	"pwr_ctl pcie_D on/off\r\n"
);

LYNXI_MCU_CMD(
	pmic, MAX_CMD_ARG_NUM, 1, do_pmic_ctl,
	"PMIC MP5416 control command",
	"pmic reset    - Reset MP5416\r\npmic rd_vol   - Read MP5416's voltage\r\n"
	"pmic set  [channal] [voltage]\r\n"
);

LYNXI_MCU_CMD(
	vol, MAX_CMD_ARG_NUM, 1, do_vol,
	"Read Chip INA220's voltage.",
	"vol rd_all"
);

LYNXI_MCU_CMD(
	pwr_en, MAX_CMD_ARG_NUM,1, do_power_enable,
	"Enable all power .",
	"pwr_en"
);

LYNXI_MCU_CMD(
	pwr_dis, MAX_CMD_ARG_NUM,1, do_power_disable,
	"Disable all power .",
	"pwr_dis"
);


